﻿using System.ComponentModel.Composition;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using DeepZoomPivotConstructor.VisualLib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RenderEngine;
using RenderEngineInterfaces;
using SimpleFileRendering;

namespace t_SimpleFileRendering
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class t_StringToDirInfoRender
    {
        public t_StringToDirInfoRender()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [TestInitialize]
        public void InitForTest()
        {
            t_TestingLibrary.Utilities.MyClassInit();
            t_TestingLibrary.Utilities.AddPart(new RenderControl());
            t_TestingLibrary.Utilities.AddPart(new LayoutHelper());
        }

        [TestCleanup]
        public void ShutDownTest()
        {
            t_TestingLibrary.Utilities.MyClassDone();
        }

        [TestMethod]
        public void TestNonExistantDirectoryPriority()
        {
            StringToDirInfoRender sf = new StringToDirInfoRender();
            t_TestingLibrary.Utilities.Compose(sf);
            Assert.AreEqual(RenderPluginPriority.RPPCantDoIt, sf.RenderPriority("f:\\bogusdude"));
        }

        [TestMethod]
        public void TestFileThatExistsPriority()
        {
            StringToDirInfoRender sf = new StringToDirInfoRender();
            t_TestingLibrary.Utilities.Compose(sf);

            DirectoryInfo info = new DirectoryInfo("TestFileThatExistsPriority");
            info.Create();

            Assert.AreEqual(RenderPluginPriority.RPPGeneric, sf.RenderPriority(info.FullName));
        }

        [TestMethod]
        public void TestDirRelativeNoGo()
        {
            StringToDirInfoRender sf = new StringToDirInfoRender();
            t_TestingLibrary.Utilities.Compose(sf);

            DirectoryInfo info = new DirectoryInfo("TestDirRelativeNoGo");
            info.Create();

            Assert.AreEqual(RenderPluginPriority.RPPCantDoIt, sf.RenderPriority(info.Name));
        }

        [TestMethod]
        public void TestDirRelativeGo()
        {
            StringToDirInfoRender sf = new StringToDirInfoRender();
            PrepareObject(sf);

            DirectoryInfo info = new DirectoryInfo("TestDirRelativeGo");
            info.Create();

            Assert.AreEqual(RenderPluginPriority.RPPGeneric, sf.RenderPriority(info.Name));
        }

        [TestMethod]
        public void TestFileThatExistsRendering()
        {
            StringToDirInfoRender sf = new StringToDirInfoRender();
            t_TestingLibrary.Utilities.AddPart(new JPEGFileRender());
            t_TestingLibrary.Utilities.AddPart(new RenderDirInfo());
            t_TestingLibrary.Utilities.Compose(sf);

            DirectoryInfo info = new DirectoryInfo("TestFileThatExistsRendering");
            info.Create();

            var vfuture = sf.Render(info.FullName);
            Assert.IsNotNull(vfuture, "Error - the future came back null!");
            Visual v = vfuture.Result;
            Assert.IsNotNull(v, "The visual should have come back from the rendering");
            Assert.AreEqual(1, v.SGNodeList.Count, "Wrong number of actual images");
        }

        [Import]
        public IRenderEngine MyRender { get; set; }

        [Export(typeof(IRenderItem))]
        [RenderInfo(typeof(DirectoryInfo))]
        public class RenderDirInfo : IRenderItem
        {
            public System.Threading.Tasks.Task<Visual> Render(object what)
            {
                return Task<Visual>.Factory.StartNew(() =>
                    {
                        return new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
                    });
            }

            public RenderPluginPriority RenderPriority(object what)
            {
                if (what.GetType() == typeof(DirectoryInfo))
                {
                    return RenderPluginPriority.RPPGeneric;
                }
                return RenderPluginPriority.RPPCantDoIt;
            }
        }


        [TestMethod]
        public void TestMetaData()
        {
            t_TestingLibrary.Utilities.AddPart(new StringToDirInfoRender());
            t_TestingLibrary.Utilities.AddPart(new RenderDirInfo());
            PrepareObject(this);

            DirectoryInfo info = new DirectoryInfo("TestMetaData");
            info.Create();

            Visual v = MyRender.Render(info.FullName);
            Assert.IsNotNull(v, "The visual should have come back from the rendering");
            Assert.AreEqual(1, v.SGNodeList.Count, "Wrong number of actual images");
        }

        /// <summary>
        /// To get out the individual variables.
        /// </summary>
        class testme
        {
            [Export("RelativePathBase")]
            public DirectoryInfo localLocation;
        }

        /// <summary>
        /// Setup for MEF the render
        /// </summary>
        /// <param name="sf"></param>
        private void PrepareObject(object sf)
        {
            var dude = new testme() { localLocation = new DirectoryInfo(".") };
            t_TestingLibrary.Utilities.AddPart(dude);
            t_TestingLibrary.Utilities.Compose(sf);
        }
    }
}
